package com.sunday.fangdaifu.base;

import android.os.StrictMode;
import android.util.Log;


import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.google.gson.Gson;
import com.sunday.common.base.BaseApplication;

import com.sunday.common.utils.ToastUtils;
import com.sunday.fangdaifu.common.Const;

import com.sunday.fangdaifu.model.Member;
import com.sunday.fangdaifu.service.LocationService;
import com.sunday.fangdaifu.utils.SharePerferenceUtils;


/**
 * Created by Administrator on 2015/6/9.
 */
public class MyApplication extends BaseApplication implements Thread.UncaughtExceptionHandler {


    private static MyApplication instance;


    public static boolean isLogin;

    private Member member;
    public LocationService locationService;
    private LocationClient mClient;
    protected  static final String TAG = "MyApplication";
    private Gson gson;
    private boolean isLocatioSuc;

    public synchronized static MyApplication getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
        SDKInitializer.initialize(getApplicationContext());
        instance = this;
        locationService = new LocationService(getApplicationContext());
    }

    public void requestLocation() {
        LocationClientOption mOption = locationService.getDefaultLocationClientOption();
        mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        mOption.setCoorType("bd09ll");
        mClient = new LocationClient(getApplicationContext());
        mClient.setLocOption(mOption);
        mClient.registerLocationListener(listener);
        mClient.start();
    }

    /***
     * 定位结果回调，在此方法中处理定位结果
     */
    BDLocationListener listener = new BDLocationListener() {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // TODO Auto-generated method stub

            if (location != null && location.getLocType() != BDLocation.TypeServerError) {
                Log.i(TAG, "定位成功");
                Log.i(TAG, "currentThread" + Thread.currentThread());
                mClient.stop();
                String latitude = String.valueOf(location.getLatitude());
                String longitude = String.valueOf(location.getLongitude());
                SharePerferenceUtils.getIns().putString("latitude", latitude);
                SharePerferenceUtils.getIns().putString("longitude", longitude);
                isLocatioSuc=true;
            } else {
                isLocatioSuc=false;
                ToastUtils.showToast(getApplicationContext(), "定位失败，请开启位置服务后重新定位");
            }
        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }
    };


    public static String getMemberId() {
//    if(memberId==null||memberId==""){
        String memberId = String.valueOf(SharePerferenceUtils.getIns().getLong(Const.RECORD_MEMBER_ID, 0));
//    }
        return memberId;
    }

    public Gson getGson() {
        if (gson == null) {
            gson = new Gson();
        }
        return gson;
    }



    public Member getMember() {
        return member;

    }

    public void setMembe(Member member) {
        this.member = member;


    }


    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace();
        //ToastUtils.showToast(this,ex.getMessage());
      /*  if(!BuildConfig.DEBUG){
            System.exit(1);
        }*/
        System.exit(1);
    }


    public boolean isLocatioSuc() {
        return isLocatioSuc;
    }

    public void setLocatioSuc(boolean locatioSuc) {
        isLocatioSuc = locatioSuc;
    }
}
